package cn.zz.camera;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.tbruyelle.rxpermissions.RxPermissions;

import cn.zz.cameralibrary.PhotoPickActivity;
import cn.zz.cameralibrary.model.CameraSdkParameterInfo;
import rx.functions.Action1;

public class MainActivity extends AppCompatActivity {

    private RadioGroup mChoiceMode, mShowCamera, mCrop;
    private EditText mRequestNum;
    private CameraSdkParameterInfo mCameraSdkParameterInfo = new CameraSdkParameterInfo();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ((TextView) findViewById(R.id.camerasdk_actionbar_title)).setText(getString(R.string.app_name));

        mChoiceMode = (RadioGroup) findViewById(R.id.choice_mode);
        mShowCamera = (RadioGroup) findViewById(R.id.show_camera);
        mRequestNum = (EditText) findViewById(R.id.request_num);
        mCrop = (RadioGroup) findViewById(R.id.rg_crop);

        mChoiceMode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                if (checkedId == R.id.multi) {
                    mRequestNum.setEnabled(true);
                } else {
                    mRequestNum.setEnabled(false);
                    mRequestNum.setText("");
                }
            }
        });

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                RxPermissions rxPermissions = new RxPermissions(MainActivity.this);
                rxPermissions.request(android.Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                        if (aBoolean) {
                            boolean mode_flag = mChoiceMode.getCheckedRadioButtonId() == R.id.single;
                            mCameraSdkParameterInfo.setSingle_mode(mode_flag);

                            boolean camera_flag = mShowCamera.getCheckedRadioButtonId() == R.id.show;
                            mCameraSdkParameterInfo.setShow_camera(camera_flag);


                            if (!TextUtils.isEmpty(mRequestNum.getText())) {
                                int maxNum = Integer.valueOf(mRequestNum.getText().toString());
                                mCameraSdkParameterInfo.setMax_image(maxNum);
                            }

                            boolean crop_flag = mCrop.getCheckedRadioButtonId() == R.id.crop_yes;
                            mCameraSdkParameterInfo.setCroper_image(crop_flag);

                            if (crop_flag) {
                                //暂时只支持单张即Single_mode模式必须为true
                                if (!mCameraSdkParameterInfo.isSingle_mode()) {
                                    Toast.makeText(MainActivity.this, "选择模式必须为单选", Toast.LENGTH_LONG).show();
                                    return;
                                }
                            }

                            mCameraSdkParameterInfo.setFilter_image(true);

                /*if(filter_flag){
                    //暂时只支持单张即Single_mode模式必须为true
                	if(!mCameraSdkParameterInfo.isSingle_mode()){
                		Toast.makeText(MainActivity.this, "选择模式必须为单选", Toast.LENGTH_LONG).show();;
                		return;
                	}
                }*/

                            Intent intent = new Intent();
                            intent.setClass(MainActivity.this, PhotoPickActivity.class);
                            Bundle b = new Bundle();
                            b.putSerializable(CameraSdkParameterInfo.EXTRA_PARAMETER, mCameraSdkParameterInfo);
                            intent.putExtras(b);
                            startActivityForResult(intent, CameraSdkParameterInfo.TAKE_PICTURE_FROM_GALLERY);
                        } else {
                            Toast.makeText(MainActivity.this, "缺少权限。", Toast.LENGTH_SHORT).show();
                        } 
                    }
                });
            }
        });

    }

    public void openFilterActivity() {

    }

    //跳转到返回结果
    public void openResultActivity() {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == CameraSdkParameterInfo.TAKE_PICTURE_FROM_GALLERY) {
            if (data != null) {
                Intent intent = new Intent(MainActivity.this, ResultActivity.class);
                intent.putExtras(data.getExtras());
                startActivity(intent);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
